#include <iostream>
#include <mysql.h>

using namespace std;

// 编译: g++ mysql1.cpp -o mysql -I/usr/include/mysql -L/usr/lib/x86_64-linux-gnu -lmysqlclient -std=c++11

int main()
{
	MYSQL *conn = mysql_init(NULL);
	auto ret = mysql_real_connect(conn, "127.0.0.1", "root", NULL, "student", 3306, NULL, CLIENT_MULTI_STATEMENTS);
	if(ret == nullptr) {
        cout << "connect failed ";
        return false;
    }

	string sql = "select * from info;";
    auto search_res = mysql_query(conn, sql.c_str());
    if(mysql_errno(conn) != 0) {
        cout << "[MySQL] " << mysql_error(conn) << ", sql: " << sql.c_str();
        return false;
    }
    auto result = mysql_store_result(conn);
    int row = mysql_num_rows(result);//获取行数
    for(int i = 0; i < row; i++) {
    	auto info=mysql_fetch_row(result);
    	cout << info[0] << "\t" << info[1] << endl;
    }

	return 0;
}